import type { SaveOrUpdateIndicatorParams } from '@/types'
import { pointApi } from '@/api'

export const AssociationModal = componentLoadAsync(
  () => import('./AssociationModal.vue'),
)

export const FormModal = componentLoadAsync(
  () => import('./FormModal.vue'),
)

export const defaultFormData: Partial<SaveOrUpdateIndicatorParams>
  = {
    pointId: 0,
    indicatorCode: '',
    indicatorName: '',
    parentId: null as any,
    indicatorType: '',
    statisticalType: '1',
    indicatorNumber: '',
    color: '',
    shape: '',
    unit: '',
    // isVirtually: 0,
    indicatorMultiple: 1,
    energyId: null as any,
    unitId: null as any,
    measuringPointNumber: '',
    source: 1,
  }

export const formRules = generateCurrencyFormRules([
  'indicatorId',
  'calculation',
])

export const colors = [
  {
    value: '#E63415',
    label: 'red',
  },
  {
    value: '#FF6600',
    label: 'orange',
  },
  {
    value: '#FFDE0A',
    label: 'yellow',
  },
  {
    value: '#1EC79D',
    label: 'green',
  },
  {
    value: '#14CCCC',
    label: 'cyan',
  },
  {
    value: '#4167F0',
    label: 'blue',
  },
  {
    value: '#6222C9',
    label: 'purple',
  },
]

export const shapes = [
  {
    value: '1',
    label: '矩形',
    type: 'rect',
    class: 'shape rect',
  },
  {
    value: '2',
    label: '圆角矩形',
    type: 'rect-with-round',
    class: 'shape rounded-rect',
  },
  {
    value: '3',
    label: '椭圆',
    type: 'ellipse',
    class: 'shape elliptic',
  },
]

/** 点位树相关 */
export function usePointTree() {
  const treeData = shallowRef<any[]>([])
  const defaultTreeExpandKeys = ref<number[]>([])
  const getTreeData = async () => {
    try {
      const { data: res } = await pointApi.loadRoleEnergyUnit()
      treeData.value = res.list || []
      if (res.pointIdList === null) {
        defaultTreeExpandKeys.value = findAllIdFromTree(res.list, [])
      }
      else {
        defaultTreeExpandKeys.value = (res.pointIdList || []).map((v: string) => Number(v))
      }
    }
    catch (e) {}
  }

  function findAllIdFromTree(tree: any[], ids: number[]) {
    tree.forEach((item) => {
      ids.push(item.id)
      if (item.children && item.children.length > 0) {
        findAllIdFromTree(item.children, ids)
      }
    })
    return ids
  }

  return {
    treeData,
    defaultTreeExpandKeys,

    getTreeData,
  }
}
